<template>
  <div>
    <search-tt @reset="reset" @search='search' :disabled='searchDisabled' :typeInfo='isMyProject?typeInfoM:typeInfo' :years='years' @statusChange='yearChange'>
      <template #right v-if="hasAuth('sys:construct:switch')">
        <el-button type="primary" @click="toGenerateProject">新建项目</el-button>
      </template>
      <template #select v-if="hasAuth('sys:construct:switch')">
        <el-select @change="statusChange" style="width: 160px;" v-model="isMyProject" placeholder="请选择项目类型">
          <el-option
            label="发布的项目"
            :value="0">
          </el-option>
          <el-option
            label="我的项目"
            :value="1">
          </el-option>
          </el-select>
      </template>
    </search-tt>
    <el-table v-loading='loading' :infinite-scroll-disabled="disabled" v-infinite-scroll="load" stripe border :data="constructList" infinite-scroll-delay='1000' style="width: 100%;" height="500" row-key="menuId">
      <el-table-column label="序号" align="center" type="index" width="70" center></el-table-column>
      <el-table-column label="项目名称"  prop="titleName"></el-table-column>
      <el-table-column label="项目类型" v-if="isMyProject===''">
        <template slot-scope='scope'>
          <span>{{scope.row.isMyProject?'我的项目':'发布的项目'}}</span>
        </template>
      </el-table-column>
      <el-table-column sortable label="创建时间" align="center" prop="createTime" ></el-table-column>
      <el-table-column label="操作" >
        <template slot-scope="scope">
          <div >
            <el-button type="text" v-if="scope.row.isMyProject" @click="$router.push('/home/projectbuild?id='+scope.row.id)">进入建设任务</el-button>
            <el-button type="text" v-else @click="editProject(scope.row)">编辑项目</el-button>
            <!-- <el-button type="text" style="color: #67C23A;" @click="exportZip(scope.row.id)">导出</el-button> -->
            <a :href="'/api/downloadZipPdf?conId='+scope.row.id" style="color: #67C23A;text-decoration: none;font-size: 16px;font-weight: 600;">导出</a>
          </div>
        </template>
      </el-table-column>
      <template slot="empty">
        {{loading?'加载中':'暂无数据'}}
      </template>
    </el-table>
  </div>
</template>

<script>
  import searchTt from "@/components/searchTt.vue"
  export default{
    data(){
      return{
        constructList:[],
        typeInfo:{
          title:'',
          page:1,
          limit:10,
          year:'',
          titleName:'',
          type:0
        },
        typeInfoM:{
          title:'',
          page:1,
          limit:10,
          year:'',
          titleName:'',
          type:0
        },
        searchDisabled:false,
        years:[],
        loading:true,
        disabled:false,
        isMyProject:'',
      }
    },
    components:{searchTt},
    methods:{
      reset(){
        this.resetInfo()
        this.isMyProject=''
        this.typeInfoM.year=''
        this.typeInfo.year=''
        this.typeInfoM.title=''
        this.typeInfo.title=''
        this.loading=true
        this.init()
      },
      async init(){
        this.typeInfo.titleName=this.typeInfo.title
        let resultM=[],resYear,result=[]
        if(this.hasAuth('sys:construct:switch')){
          if(this.isMyProject===''||this.isMyProject==0){
            result=await this.$get('/api/construct/list',this.typeInfo)
            result.data.list.map(item=>{
              item.isMyProject=0
            })
            this.constructList.push(...result.data.list)
          }
          if(this.isMyProject===''||this.isMyProject==1){
            resultM=await this.$get('/api/construct/list/user',this.typeInfoM)
            resultM.data.list.map(item=>{
              item.isMyProject=1
            })
            this.constructList.push(...resultM.data.list)
          }
          this.loading=false
          this.searchDisabled=false
        }else{
          resultM=await this.$get('/api/construct/list/user',this.typeInfo)
          resultM.data.list.map(item=>{
            item.isMyProject=1
          })
          this.constructList.push(...resultM.data.list)
          this.loading=false
          this.searchDisabled=false
        }
        let code=this.isMyProject===''?2:(this.isMyProject==0?0:1)
        resYear=await this.$get('/api/construct/years/'+code)
        if(resYear.code==200){
          this.years=resYear.data
        }
      },
      resetInfo(){
        this.constructList=[]
        this.typeInfo.page=1
      },
      search(){
        this.searchDisabled=true
        this.resetInfo()
        this.init()
      },
      // 切换项目状态回调
      statusChange(val){
        this.resetInfo()
        this.isMyProject=val
        this.loading=true
        this.init()
      },
      yearChange(val){
        this.resetInfo()
        this.loading=true
        this.typeInfo.year=val
        this.typeInfoM.year=val
        this.init()
      },
      load(){
        this.typeInfo.page++
        this.init()
      },
      async exportZip(id){
        let res=await this.$down('/api/downloadZipPdf?conId='+id,{},'.zip')
      },
      toGenerateProject(){
        // console.log(123)
        this.$store.commit('removeAnalogueTab',"/home/teamproject")
          this.$store.commit('setTabs',{
            name: "/home/teamproject",
            title: "建设项目"
        })
        this.$router.push({path:'/home/teamproject'})
      },
      editProject(row){
        this.$store.commit('removeAnalogueTab',"/home/teamproject")
        this.$store.commit('setTabs',{
          name: "/home/teamproject?id="+row.id+'&title='+row.titleName,
          title: "建设项目"
        })
        this.$router.push({path:'/home/teamproject',query:{id:row.id,title:row.titleName}})
      }
    },
    mounted(){
      this.init()
    }
  }
</script>

<style>
</style>
